// router.js
import { RouterMount, createRouter } from "uni-simple-router";

const router = createRouter({
  platform: process.env.VUE_APP_PLATFORM,
  routes: [...ROUTES],
});
//全局路由前置守卫
router.beforeEach((to, from, next) => {
  get_BK_image();
  getApp().globalData.userID = uni.getStorageSync("userID");
  if ((getApp().globalData.userID == null || getApp().globalData.userID == "" 
  || getApp().globalData.userID < 0 )&& to.path.indexOf("login") < 0) {
    console.log("用户未登录！！！");
    next({ path: "/pages/login/login", NAVTYPE: "replaceAll" });
  } else {
    console.log("用户已经登录！！！");
    next();
  }
  // next();
});
// 全局路由后置守卫
router.afterEach((to, from) => {
  console.log("跳转结束");
});

function get_BK_image() {
  uni.getSystemInfo({
    success: (res) => {
      if(res.model.search('PC') >= 0) {
        getApp().globalData.bkurl = getApp().globalData.api_root+`/img/PC/bk.jpg`
      } else if(res.model.search('iPad') >= 0) {
        getApp().globalData.bkurl = getApp().globalData.api_root+`/img/iPad/bk.jpg`
      } else if(res.model.search('iPhone') >= 0) {
        getApp().globalData.bkurl = getApp().globalData.api_root+`/img/iPhone/bk.jpg`
      } else {
        getApp().globalData.bkurl = getApp().globalData.api_root+`/img/other/bk.jpg`
      }
      // getApp().globalData.bkurl = getApp().globalData.api_root+`/upload/${res.model}/bk.jpg`
      console.log(getApp().globalData.bkurl);
      console.log(res.model);
    }
  });
};

export { router, RouterMount };
